package com.ruimei.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruimei.entitys.Leave;
import com.ruimei.entitys.Teacher;
import com.ruimei.service.LeaveService;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Api(tags={"请假"})
public class LeaveController
{
  @Autowired
  LeaveService leaveService;
  @Resource
  private  RedisTemplate<String,String> redisTemplate;
  
  @ApiOperation(value="W查询管理班级请假学生", notes="根据教师id,输入leaveId查询请假详情")
  @PostMapping({"/leave"})
  @ResponseBody
  public Map<String, Object> getLeaves(String leaveId,String classId)
  {
    Map<String, Object> map = this.leaveService.getLeaveByTeacherId(leaveId,classId);
    return map;
  }
  
  @ApiOperation(value="W查询管理班级请假学生", notes="传入leaveId,remarks(教师回复)")
  @PostMapping({"/setleave"})
  @ResponseBody
  public Map<String, Object> setLeave(Leave leave, HttpServletRequest request)
  {
    Teacher teacher = new Teacher();
    teacher.setId(TokenUtils.getTeacherId(request));
    if(redisTemplate.hasKey(teacher.getId())){
       teacher = JSONArray.parseObject(redisTemplate.opsForValue().get(teacher.getId()),Teacher.class);
      Map<String, Object> map = this.leaveService.setLeave(leave, teacher);
    return map;
    }
    return null;
  }
  
  @ApiOperation("发起请假请求")
  @PostMapping({"/leave/parent"})
  @ResponseBody
  public Map<String, Object> leaveParent(Leave leave,HttpServletRequest request)
  {
    Map<String, Object> map = new HashMap();
    if (this.leaveService.leaveParent(leave,TokenUtils.getParentId(request)))
    {
      map.put("message", "发起成功");
      map.put("status", Boolean.valueOf(true));
      return map;
    }
    map.put("message", "服务器繁忙，请稍后再试");
    map.put("status", Boolean.valueOf(true));
    return map;
  }
  
  @ApiOperation("查看请假列表")
  @PostMapping({"/leave/view"})
  @ResponseBody
  public Map<String, Object> leaveView(String id, String leaveId)
  {
    Map<String, Object> map = new HashMap();
    map.put("data", this.leaveService.leaveView(id, leaveId));
    map.put("status", Boolean.valueOf(true));
    return map;
  }
}